let game = require("Map");

cc.Class({
    extends: cc.Component,

    properties: {
        rotate: {
            default: null,
            type: cc.Node
        },
        left: {
            default: null,
            type: cc.Node,
        },
        right: {
            default: null,
            type: cc.Node
        },
        down: {
            default: null,
            type: cc.Node
        },
        home: {
            default: null,
            type: cc.Node
        },
        again: {
            default: null,
            type: cc.Node
        },
        btnSound: {
            default: null,
            type: cc.AudioClip
        },
        play: {
            default: null,
            type: game
        },
        reTime: 1000    //按键重复触发间隔ms
    },

    leftBtn(){
        cc.audioEngine.play(this.btnSound, false, lcs_BtnVol)
        this.play.left()
    },

    rightBtn(){
        cc.audioEngine.play(this.btnSound, false, lcs_BtnVol)
        this.play.right()
    },

    touchUp(e){
        clearTimeout(e.target.press)
        clearInterval(e.target.press2)
    },

    onLoad () {

        this.left.on('touchstart', function(e){
            this.leftBtn()
            e.target.press = setTimeout(function(){
                this.leftBtn()
                e.target.press2 = setInterval(function(){
                    this.leftBtn()
                }.bind(this), 100)
            }.bind(this), this.reTime)
        }, this)

        this.right.on('touchstart', function(e){
            this.rightBtn()
            e.target.press = setTimeout(function(){
                this.rightBtn()
                e.target.press2 = setInterval(function(){
                    this.rightBtn()
                }.bind(this), 100)
            }.bind(this), this.reTime)
        }, this)

        function pressUp(e){
            clearTimeout(e.target.press)
            clearInterval(e.target.press2)
        }

        this.left.on('touchend', pressUp, this)
        this.left.on('touchcancel', pressUp, this)
        this.right.on('touchend', pressUp, this)
        this.right.on('touchcancel', pressUp, this)

        
        this.home.on('touchstart', function(){
            cc.director.loadScene('Menu')
        }, this)

        this.again.on('touchstart', function(){
            cc.director.loadScene('Game')
        }, this)

        this.rotate.on('touchstart', function(){
            cc.audioEngine.play(this.btnSound, false, lcs_BtnVol)
            this.play.rotate()
        }, this)

        this.down.on('touchstart', function(){
            cc.audioEngine.play(this.btnSound, false, lcs_BtnVol)
            this.play.down(true)
        }, this)

        
        this.rotate.on('size-changed', function(){
            // let padding = (this.node.height-this.rotate.height) / 2

            this.rotate.height = this.rotate.width
            this.rotate.getComponent('cc.Widget').bottom = (this.node.height-this.rotate.height) / 2
            this.rotate.getComponent('cc.Widget').updateAlignment()

            this.left.height = this.left.width
            this.left.getComponent('cc.Widget').bottom = (this.node.height-this.rotate.height) / 2
            this.left.getComponent('cc.Widget').updateAlignment()
            
            this.right.height = this.right.width
            this.right.getComponent('cc.Widget').bottom = (this.node.height-this.rotate.height) / 2
            this.right.getComponent('cc.Widget').updateAlignment()
            
            this.down.height = this.down.width
            this.down.getComponent('cc.Widget').bottom = (this.node.height-this.rotate.height) / 2
            this.down.getComponent('cc.Widget').updateAlignment()
        }, this)

    },

    // start () {},

    // update (dt) {},
});
